import 'package:flutter/material.dart';

///appBar
///bottom -- android中tabLayout和ViewPager的使用
class TabBarDemo extends StatefulWidget {
  // const TabBarDemo({Key? key}) : super(key: key);
  List widgets = [FlutterView(), AndroidView(), IOSView()];

  @override
  _TabBarDemoState createState() => _TabBarDemoState();
}

class _TabBarDemoState extends State<TabBarDemo>
    with SingleTickerProviderStateMixin {
  final List tabs = ["flutter", "Android", "IOS"];

  //tab的控制器
  late TabController _controller;
  int _index = 0;

  @override
  void initState() {
    ///这句的{this 需要配合 @link with  SingleTickerProviderStateMixin}的使用
    _controller = TabController(length: tabs.length, vsync: this);
    _controller.addListener(() {
      setState(() {
        _index = _controller.index;
      });
    });
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text("tabBar"),
        leading: IconButton(
          icon: Icon(Icons.home),
          onPressed: () {
            print("leading");
          },
        ),
        actions: [
          IconButton(
              onPressed: () {
                print("actions");
              },
              icon: Icon(Icons.add))
        ],
        bottom: TabBar(
          controller: _controller,
          tabs: tabs.map((e) => Tab(child: Text("$e"))).toList(),
        ),
      ),
      body: widget.widgets[_index],
    );
  }
}

///
class FlutterView extends StatelessWidget {
  const FlutterView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text("Flutter"),
    );
  }
}

///
class AndroidView extends StatelessWidget {
  const AndroidView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text("Android"),
    );
  }
}

///
class IOSView extends StatelessWidget {
  const IOSView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text("IOS"),
    );
  }
}
